	

package com.cpi.cornpp.purchaseinstructiondetail.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.conrpp.mail.Email;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customer.exception.CustomerException;
import com.cpi.cornpp.customer.services.CustomerServices;
import com.cpi.cornpp.customer.view.CustomerView;
import com.cpi.cornpp.instructions.futures.exception.InstructionsFuturesException;
import com.cpi.cornpp.instructions.futures.services.InstructionsFuturesService;
import com.cpi.cornpp.instructions.futures.view.InstructionsFuturesView;
import com.cpi.cornpp.instructionsforecasts.exception.InstructionsForecastsException;
import com.cpi.cornpp.instructionsforecasts.service.InstructionForecastsService;
import com.cpi.cornpp.instructionsforecasts.view.InstructionsForecastsDetailView;
import com.cpi.cornpp.instructionspurchasedetail.form.InstructionPurchaseDetailForm;
import com.cpi.cornpp.instructionspurchases.service.InstructionPurchaseMailService;
import com.cpi.cornpp.instructionspurchases.service.InstructionPurchaseService;
import com.cpi.cornpp.instructionspurchases.view.InstructionPurchaseCreateView;
import com.cpi.cornpp.period.exception.PeriodException;
import com.cpi.cornpp.product.exception.ProductException;
import com.cpi.cornpp.product.services.ProductService;
import com.cpi.cornpp.product.view.ProductView;
import com.cpi.cornpp.purchaseinstructiondetail.exception.InstructionPurchaseDetailException;
import com.cpi.cornpp.purchaseinstructiondetail.service.InstructionPurchaseDetailService;
import com.cpi.cornpp.purchaseinstructiondetail.view.InstructionPurchaseDetailView;
import com.cpi.cornpp.unit.exception.UnitException;
import com.cpi.cornpp.unit.service.UnitService;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;

public class InstructionPurchaseDetailAction extends MappingDispatchAction{
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String FAILURE_FORWARD = "fail";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "default";//$NON-NLS-1$
	private String PURCHASE_INSTRUCTION_DETAILS = "PURCHASE_INSTRUCTION_DETAILS";
	private static final String PURCHASE_INSTRUCTION_ID = "purchaseInstructionId";//$NON-NLS-1$;
	private static final String PID = "pid";	
	private SystemParameterCollection systemParameterCollection = null;
	private SystemParameterBean systemParameterBean = null;
	private static final String RECORDS_TO_PURCHASES_INSTRUCTIONS_FUTURES = "RECORDS_TO_PURCHASES_INSTRUCTIONS_FUTURES";
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_UPDATE_LABEL = "label.purchases.instructions.save.success";
	private static final String SUCCESS_INSERT_LABEL = "label.purchases.instructions.save.success";
	private String CUSTOMER_VIEW = "CustomerView";//$NON-NLS-1$
	private static final String MESSAGE_SELECT_A_CLIENT = "error.message.futurepurchase.add.customers";//$NON-NLS-1$
	private static final String MESSAGE_SELECT_A_PRODUCT = "error.message.futurepurchase.select.product";//$NON-NLS-1$
	private static final String CON = "CON";
	private static final String CON_UNIT = "CON_UNIT";
	private static final String INSTRUCTION_PURCHASE_STATUS_OPENED_ID = "INSTRUCTION_PURCHASE_STATUS_OPENED_ID";
	private static final String READ_ONLY = "READ_ONLY";
	private final static String CUSTOMER_ID_LABEL = "customerId";//$NON-NLS-1$
	private static final String PRODUCTS_CUSTOMER_ID = "productsCustomerId";//$NON-NLS-1$1$
	private static final String CUSTOMER_PRODUCTS = "listOfProducts";//$NON-NLS-1$1$
	private static final String SELECTED_PRODUCTS = "customerSelectedProducts";//$NON-NLS-1$
	private static final String NSELECTED_PRODUCTS = "nSelectedProducts";//$NON-NLS-1$
	/**
	 * Lista de instruction purchases
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 */
	public ActionForward list(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws InstructionPurchaseDetailException{
		ActionForward forward = null;
		InstructionPurchaseDetailService service = null;				
		ArrayList listPurchaseInstructionDetails = null;
		HttpSession session = null;
		MessageResources messageResources = null;
		String messageError = null;
		String purchaseInstructionParam = null;
		int purchaseInstructionId = -1;
		
		purchaseInstructionParam = request.getParameter("intforecast");		
		if(purchaseInstructionParam!=null ){
			purchaseInstructionId = NumberUtil.parseInt(purchaseInstructionParam);
		}
		session = request.getSession(false);
		messageResources = getResources(request);
		service = new InstructionPurchaseDetailService();
		
		try {
			listPurchaseInstructionDetails = (ArrayList)service.list(purchaseInstructionId);
			request.setAttribute(PURCHASE_INSTRUCTION_DETAILS , listPurchaseInstructionDetails);
		}catch(InstructionPurchaseDetailException e){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), e.getErrorCode());
			
			if(null == messageError){
				InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e, InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_LISTS);
				session.setAttribute(CUSTOM_EXCEPTION, pie);
				//form.reset(mapping, request);
				throw pie;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
		} catch(Exception e){
			InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e , InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_LISTS);;
			
			session.setAttribute(CUSTOM_EXCEPTION, pie);
			//form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw pie;
		}		
		
		forward = mapping.findForward(SUCCESS_FORWARD);
		return forward;		
	}
	
	/**
	 * Se dirige a la pantalla de edicion del instruction purchase
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws InstructionPurchaseDetailException, InstructionsForecastsException{
		InstructionPurchaseDetailForm form = null;		
		InstructionPurchaseDetailView view = null;
		InstructionsForecastsDetailView foreCastView = null;
		InstructionPurchaseDetailService service = null;
		InstructionForecastsService forecastService = null;
		InstructionsFuturesService futureService = null;
		UnitService unitService = null;
		String paramId = null;
		int purchaseInstructionDetailId;
		HttpSession session = null;
		MessageResources messageResources = null;
		String messageError = null;
		List purchaseDetails = null;
		List futuresView = null;
		
		session = request.getSession();
		messageResources = getResources(request);
		service = new InstructionPurchaseDetailService();
		forecastService = new InstructionForecastsService();
		futureService = new InstructionsFuturesService();
		
		InstructionPurchaseService instructionPurchaseService = null;
		InstructionPurchaseCreateView instructionPurchaseCreateView = null;
		
		foreCastView = new InstructionsForecastsDetailView();
		try{
			form = (InstructionPurchaseDetailForm)actionForm;
			paramId = request.getParameter(PID);
			if( paramId == null ){
				paramId = (String)session.getAttribute(PID);
			}else{
				session.setAttribute(PID, paramId);
			}
			unitService = new UnitService();
			instructionPurchaseService = new InstructionPurchaseService();
			
			purchaseInstructionDetailId = NumberUtil.parseInt(paramId);							
			view = service.getById(purchaseInstructionDetailId);
			form.setIpdView(view);
			session.setAttribute(PURCHASE_INSTRUCTION_ID, String.valueOf(view.getPurchaseInstructionId()));
			session.setAttribute(SELECTED_PRODUCTS, view.getProductList());
			purchaseDetails = forecastService.list((List)view.getProductList(), view.getCustomerView(), view.getPurchaseInstructionDetailid(),-1,-1);
			foreCastView.setDetails(purchaseDetails);
			form.setInstructionsForecastsDetailView(foreCastView);
			futuresView = futureService.getInstructionsFuturesList(purchaseInstructionDetailId);
			
			systemParameterCollection = SystemParameterCollection.getInstance();
			SystemParameterBean systemParameterOpenedBean = systemParameterCollection.getSystemParameterBean(INSTRUCTION_PURCHASE_STATUS_OPENED_ID);
			instructionPurchaseCreateView = instructionPurchaseService.getInstructionPurchase(view.getPurchaseInstructionId());
			if( instructionPurchaseCreateView.getPurchaseStatus() != systemParameterOpenedBean.getIntValue() ){
				request.setAttribute(READ_ONLY, String.valueOf(true));
			}else{
				futuresView = fillList(futuresView, purchaseInstructionDetailId,futureService);
				request.setAttribute(READ_ONLY, String.valueOf(false));
			}
			form.setInstructionsFuturesView(futuresView);
			session.setAttribute(CON_UNIT, String.valueOf(unitService.getUnitQuantity(CON)));
		} catch(InstructionPurchaseDetailException e){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), e.getErrorCode());			
			if(null == messageError){
				InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e, InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_SELECT);
				session.setAttribute(CUSTOM_EXCEPTION, pie);				
				throw pie;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
			}
		} catch(InstructionsForecastsException ife){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), ife.getErrorCode());
			
			if(null == messageError){
				InstructionsForecastsException ifee = new InstructionsForecastsException(ife, InstructionsForecastsException.LAYER_ACTION, InstructionsForecastsException.ACTION_LISTS);
				session.setAttribute(CUSTOM_EXCEPTION, ifee);				
				throw ifee;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);				
			}
		} catch(Exception e){
			InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e , InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_SELECT);			
			session.setAttribute(CUSTOM_EXCEPTION, pie);			
			e.printStackTrace(System.out);			
			throw pie;
		}		
		return mapping.findForward(SUCCESS_FORWARD);
	}
	
	/**
	 * Actualiza un {@link InstructionPurchaseDetailView}
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 */
	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws InstructionPurchaseDetailException,InstructionsFuturesException{
		InstructionPurchaseDetailForm form = null;		
		InstructionPurchaseDetailView view = null;
		InstructionPurchaseDetailService service = null;
		InstructionsFuturesService futureService = null;
		InstructionForecastsService forecastService = null;
		HttpSession session = null;
		MessageResources messageResources = null;
		String messageError = null;
		String forward = SUCCESS_FORWARD;
		InstructionPurchaseMailService mailService = null;
		Email email = null;
		Locale locale = null;
		
		session = request.getSession();
		messageResources = getResources(request);
		service = new InstructionPurchaseDetailService();
		forecastService = new InstructionForecastsService();
		futureService = new InstructionsFuturesService();
		try{
			
			form = (InstructionPurchaseDetailForm)actionForm;
			view = form.getIpdView();
			service.update(view);
			forecastService.updateInstructionsForecastsViews(form.getInstructionsForecastsDetailView().getDetails(), form.getInstructionsForecastsDetailView().getChecked(),view.getPurchaseInstructionDetailid());
			futureService.saveInstructionsFutures(form.getInstructionsFuturesView());
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_UPDATE_LABEL);
			session.setAttribute(MESSAGE, messageError);

			locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
			if(request.getParameter("sendEmail")!=null){
				mailService = new InstructionPurchaseMailService();					
				email = mailService.buildMailForAddOrUpdateAction(form.getIpdView().getPurchaseInstructionId(),messageResources, locale, false);
				mailService.sendEmail(email);
			}
			
			
		}catch(InstructionPurchaseDetailException e){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), e.getErrorCode());			
			if(null == messageError){
				InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e, InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_SELECT);
				session.setAttribute(CUSTOM_EXCEPTION, pie);				
				throw pie;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAILURE_FORWARD;
			}
		} catch (InstructionsFuturesException instructionsFuturesException) {
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), instructionsFuturesException.getErrorCode());			
			if(null == messageError){
				InstructionsFuturesException pie = new InstructionsFuturesException(instructionsFuturesException, InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_SELECT);
				session.setAttribute(CUSTOM_EXCEPTION, pie);				
				throw pie;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAILURE_FORWARD;
			}
		} catch(Exception e){
			InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e , InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_SELECT);			
			session.setAttribute(CUSTOM_EXCEPTION, pie);			
			e.printStackTrace(System.out);
			
			throw pie;
		}		
		return mapping.findForward(forward);
	}	
	
	/**
	 * Helper Utilizado para llenar la lista
	 * de vistas InstrucionsFuturesView
	 * 
	 * @author Edgar Joao
	 * @param instructionsFuturesView
	 * @param futureService 
	 * @return
	 */
	private List fillList(List instructionsFuturesView, int purchaseInstructionDetailId, InstructionsFuturesService futureService) throws PeriodException {
		List list = null;
		list = new ArrayList(0);
		int totalRecordsToAdd = 0;
		InstructionsFuturesView iFuturesView = null;
		systemParameterCollection = SystemParameterCollection.getInstance();
		systemParameterBean = systemParameterCollection.getSystemParameterBean(RECORDS_TO_PURCHASES_INSTRUCTIONS_FUTURES);
		if(instructionsFuturesView!=null){		
			list.addAll(instructionsFuturesView);
			totalRecordsToAdd = systemParameterBean.getIntValue() - instructionsFuturesView.size();
		}else{
			totalRecordsToAdd = systemParameterBean.getIntValue();
		}
		for( int int_i = totalRecordsToAdd; int_i>0; int_i-- ){
			iFuturesView = new InstructionsFuturesView();
			iFuturesView.setPurchaseInstructionDetailId(purchaseInstructionDetailId);
			iFuturesView.setAccountPeriod(futureService.periodAccountApplication());
			iFuturesView.setApplicationPeriod(futureService.periodAccountApplication());
			list.add(iFuturesView);
		}		
		return list;
	}

	public ActionForward clearNewDetail(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws InstructionPurchaseDetailException, InstructionsForecastsException,UnitException, PeriodException{
		
		HttpSession httpSession = null;
		InstructionPurchaseDetailForm form = null;
		InstructionsForecastsDetailView instructionsForecastsDetailView = null;
		String piParam = null;
		InstructionPurchaseDetailView ipdView = null;
		InstructionsFuturesService futureService = null;
		List futuresView = null;
		
		httpSession = request.getSession();
		
		UnitService unitService = null;
		unitService = new UnitService();
		futureService = new InstructionsFuturesService();
		httpSession.setAttribute(CON_UNIT, String.valueOf(unitService.getUnitQuantity(CON)));
		
		piParam = request.getParameter(PURCHASE_INSTRUCTION_ID);
		
		if(piParam!=null){			
			httpSession.removeAttribute(CUSTOMER_VIEW);
			httpSession.removeAttribute(CUSTOMER_PRODUCTS);
			httpSession.removeAttribute(SELECTED_PRODUCTS);
			httpSession.removeAttribute(NSELECTED_PRODUCTS);
			httpSession.removeAttribute(PRODUCTS_CUSTOMER_ID);
			
			form = (InstructionPurchaseDetailForm)actionForm;
			ipdView = new InstructionPurchaseDetailView();
			form.setIpdView(ipdView);
			instructionsForecastsDetailView = form.getIpdView().getInstructionsForecastsDetailView();
			if(instructionsForecastsDetailView!=null){
				instructionsForecastsDetailView.setDetails(new ArrayList(0));
			}
			
			ipdView.setPurchaseInstructionId(Integer.parseInt(piParam));
			futuresView = fillList(null, -1,futureService);
			form.getIpdView().setInstructionsFuturesView(futuresView);
			
		}		
		
		return mapping.findForward(SUCCESS_FORWARD);
		
		
	}
	
	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 * @throws UnitException 
	 */
	public ActionForward newDetail(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws InstructionPurchaseDetailException, InstructionsForecastsException, PeriodException{
		InstructionPurchaseDetailForm form = null;		
		InstructionPurchaseDetailView ipdView = null;
		InstructionsForecastsDetailView foreCastView = null;
		InstructionForecastsService forecastService = null;
		InstructionsFuturesService futureService = null;
		
		CustomerView customerView = null;
		HttpSession httpSession = null;
		List purchaseDetails =  null; 
		
		form = (InstructionPurchaseDetailForm)actionForm;
		
		httpSession = request.getSession();
		customerView = (CustomerView)httpSession.getAttribute(CUSTOMER_VIEW);

		ipdView = form.getIpdView();
		if(customerView!=null){
			ipdView.getCustomerView().setCustomerId(customerView.getCustomerId());
			ipdView.getCustomerView().setDescription(customerView.getDescription());
		}else{
			customerView = new CustomerView();
			ipdView.setCustomerView(customerView);
			ipdView.getCustomerView().setCustomerId("-1");
			ipdView = form.getIpdView();
			ipdView.setCustomerView(new CustomerView());
		}
		forecastService = new InstructionForecastsService();
		purchaseDetails = forecastService.list((List)httpSession.getAttribute(SELECTED_PRODUCTS), customerView, -1, -1, -1);
		foreCastView = form.getIpdView().getInstructionsForecastsDetailView();
		foreCastView.setDetails(purchaseDetails);
		futureService = new InstructionsFuturesService();
		form.getIpdView().setInstructionsFuturesView(fillList(null, -1,futureService));		
		return mapping.findForward(SUCCESS_FORWARD);
	}

	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 */
	public ActionForward saveDetail(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws InstructionPurchaseDetailException, InstructionsForecastsException{
		InstructionPurchaseDetailForm form = null;		
		InstructionPurchaseDetailView ipdView = null;
		InstructionPurchaseDetailService purchaseDetailservice = null;
		InstructionsFuturesService futureService = null;
		InstructionForecastsService forecastService = null;
		HttpSession session = null;
		MessageResources messageResources = null;
		String messageError = null;
		InstructionPurchaseMailService mailService = null;
		Email email = null;
		Locale locale; 
		session = request.getSession();
		messageResources = getResources(request);
		purchaseDetailservice = new InstructionPurchaseDetailService();
		forecastService = new InstructionForecastsService();
		futureService = new InstructionsFuturesService();
		try{
			
			form = (InstructionPurchaseDetailForm)actionForm;
			ipdView = form.getIpdView();
			ipdView.setProductList((List)session.getAttribute(SELECTED_PRODUCTS));
			ipdView.getInstructionsForecastsDetailView().setDetails(forecastService.list((List)ipdView.getProductList(), ipdView.getCustomerView(), ipdView.getPurchaseInstructionDetailid(), -1, -1));
			purchaseDetailservice.save(ipdView);
			forecastService.updateInstructionsForecastsViews(form.getIpdView().getInstructionsForecastsDetailView().getDetails(), form.getIpdView().getInstructionsForecastsDetailView().getChecked(),ipdView.getPurchaseInstructionDetailid());
			fillInstructionFuturesParentId(form.getIpdView().getInstructionsFuturesView(),ipdView.getPurchaseInstructionDetailid());
			futureService.saveInstructionsFutures(form.getIpdView().getInstructionsFuturesView());
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_INSERT_LABEL);
			session.setAttribute(MESSAGE, messageError);

			if(request.getParameter("sendEmail")!=null){
				mailService = new InstructionPurchaseMailService();
				locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
				email = mailService.buildMailForAddOrUpdateAction(form.getIpdView().getPurchaseInstructionId(),messageResources, locale, false);
				mailService.sendEmail(email);
			}

			
		}catch(InstructionPurchaseDetailException e){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), e.getErrorCode());			
			if(null == messageError){
				InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e, InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_SELECT);
				session.setAttribute(CUSTOM_EXCEPTION, pie);				
				throw pie;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);				
			}
		} catch(Exception e){
			InstructionPurchaseDetailException pie = new InstructionPurchaseDetailException(e , InstructionPurchaseDetailException.LAYER_ACTION, InstructionPurchaseDetailException.ACTION_SELECT);			
			session.setAttribute(CUSTOM_EXCEPTION, pie);			
			e.printStackTrace(System.out);
			
			throw pie;
		}
		
		return mapping.findForward(SUCCESS_FORWARD);
	}

	/**
	 * 
	 * @param instructionsFuturesView
	 * @param purchaseInstructionDetailid
	 */
	private void fillInstructionFuturesParentId(List instructionsFuturesView, int purchaseInstructionDetailid) {
		for (Iterator iter = instructionsFuturesView.iterator(); iter.hasNext();) {
			InstructionsFuturesView view = (InstructionsFuturesView) iter.next();
			view.setPurchaseInstructionDetailId(purchaseInstructionDetailid);
		}
		
	}

	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 */
	public ActionForward searchCustomer(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws CustomerException {
		InstructionPurchaseDetailForm instructionPurchaseDetailForm = null;
		CustomerView customerView = null;
		String customerId = null;
		MessageResources messageResources = null;
		String messageError = null;
		HttpSession httpSession = null;
		CustomerServices customerServices = null;
		Collection listOfCustomers = null;
		try{
			listOfCustomers = new ArrayList(0);
			customerServices = new CustomerServices();
			httpSession = request.getSession();
			messageResources = getResources(request);
			instructionPurchaseDetailForm = (InstructionPurchaseDetailForm) actionForm;
			customerId = request.getParameter(CUSTOMER_ID_LABEL);
			customerView = new CustomerView();
			customerView.setCustomerId(org.apache.commons.lang.StringUtils.EMPTY);
			
			if( org.apache.commons.lang.StringUtils.isEmpty(customerId) ){
				customerView.setDescription(org.apache.commons.lang.StringUtils.EMPTY);
				instructionPurchaseDetailForm.getIpdView().setCustomerViewSearch(customerView);
			}else{
				customerView.setDescription(customerId);
				instructionPurchaseDetailForm.getIpdView().setCustomerViewSearch(customerView);
				listOfCustomers = new ArrayList();
				listOfCustomers =  customerServices.getCustomersList(instructionPurchaseDetailForm.getIpdView().getCustomerViewSearch());
				request.setAttribute("listOfCustomers", listOfCustomers);
				instructionPurchaseDetailForm.getIpdView().setResultCustomerViews(listOfCustomers);
			}
		}catch (CustomerException customerException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), customerException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, customerException);
				instructionPurchaseDetailForm.reset(mapping, request);
				throw customerException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw customerException;
		}
		catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		return mapping.findForward(SUCCESS_FORWARD);
	} 
	
	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 */
	public ActionForward searchCustomerResult(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws CustomerException {
		InstructionPurchaseDetailForm form = null;		
		CustomerServices customerServices = null;
		Collection listOfCustomers = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;		
		
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			form = (InstructionPurchaseDetailForm) actionForm;
			customerServices = new CustomerServices();
			
			listOfCustomers = new ArrayList();
			listOfCustomers =  customerServices.getCustomersList(form.getIpdView().getCustomerViewSearch());
			request.setAttribute("listOfCustomers", listOfCustomers);
			form.getIpdView().setResultCustomerViews(listOfCustomers);
			
		}catch (CustomerException customerException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), customerException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, customerException);
				//form.reset(mapping, request);
				throw customerException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw customerException;
		}
		catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw customerException;
		}
	return mapping.findForward(SUCCESS_FORWARD);
	} 
	
	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 */
	public ActionForward selectCustomer(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws CustomerException {		
		InstructionPurchaseDetailForm instructionPurchaseDetailForm = null;					
		CustomerView customerView = null;					
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;	
		
		try{
			messageResources = getResources(request);
			httpSession = request.getSession();
			instructionPurchaseDetailForm = (InstructionPurchaseDetailForm) actionForm;
			
			if(instructionPurchaseDetailForm.getIpdView().getCustomerViewSearch().getCustomerId() != null){								
				if (!CollectionUtils.isEmpty(instructionPurchaseDetailForm.getIpdView().getResultCustomerViews())){
					Iterator iterator = instructionPurchaseDetailForm.getIpdView().getResultCustomerViews().iterator();
					while (iterator.hasNext()) {
						CustomerView view = (CustomerView) iterator.next();
						if(org.apache.commons.lang.StringUtils.equals(view.getCustomerId().trim(), instructionPurchaseDetailForm.getIpdView().getCustomerViewSearch().getCustomerId().trim())){
							customerView = new CustomerView();
							customerView.setCustomerId(view.getCustomerId());
							customerView.setDescription(view.getDescription());						
							httpSession = request.getSession(false);
							httpSession.setAttribute(CUSTOMER_VIEW , customerView);
							searchCustomerProducts(request, view.getCustomerId());
							break;
						}
					}
				}
			}else{
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), MESSAGE_SELECT_A_CLIENT);
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
						
		}catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw customerException;
		}										
		return mapping.findForward(SUCCESS_FORWARD);
	}
	
	private void searchCustomerProducts(HttpServletRequest request, String customerId) throws Exception{
		HttpSession httpSession = request.getSession();
		String currentCustomer = (String)httpSession.getAttribute(PRODUCTS_CUSTOMER_ID);
		Collection listOfProducts = null;
		ProductService productService = null;
		if(currentCustomer==null || !currentCustomer.equals(request.getParameter(CUSTOMER_ID_LABEL))){
			productService = new ProductService();
			listOfProducts =  productService.getCurrentForescastedProductList(customerId);
			httpSession.setAttribute(CUSTOMER_PRODUCTS, listOfProducts);
			httpSession.setAttribute(SELECTED_PRODUCTS, listOfProducts);
			httpSession.setAttribute(NSELECTED_PRODUCTS, new Integer(listOfProducts.size()));
			httpSession.setAttribute(PRODUCTS_CUSTOMER_ID, customerId);
		}
	}
	
	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 */
	public ActionForward searchProduct(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws ProductException {
		InstructionPurchaseDetailForm instructionPurchaseDetailForm = null;
		HttpSession httpSession = null;
		try{
			httpSession = request.getSession();
			searchCustomerProducts(request,request.getParameter(CUSTOMER_ID_LABEL));
			instructionPurchaseDetailForm = (InstructionPurchaseDetailForm)actionForm;
			instructionPurchaseDetailForm.getIpdView().setProductList((List)httpSession.getAttribute(CUSTOMER_PRODUCTS));
			List selectedProducts = (List)httpSession.getAttribute(SELECTED_PRODUCTS);
			if(selectedProducts!=null && selectedProducts.size()>0){
				String[] idsSelectedProducts = new String[selectedProducts.size()];
				Iterator iterator = selectedProducts.iterator();
				int i=0;
				while (iterator.hasNext()) {
					ProductView product = (ProductView) iterator.next();
					idsSelectedProducts[i++]=product.getProductId();
				}
				instructionPurchaseDetailForm.getIpdView().setSelectedProducts(idsSelectedProducts);
			}
		}catch (ProductException productException){
			MessageResources messageResources = getResources(request);
			String messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), productException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, productException);
				//instructionPurchaseDetailForm.reset(mapping, request);
				throw productException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw productException;
		}
		catch (Exception exception){
			ProductException productException=null;
			exception.printStackTrace(System.out);
			productException= new ProductException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw productException;
		}
		return mapping.findForward(SUCCESS_FORWARD);
	} 
	
	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 */
	public ActionForward searchProductResult(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws ProductException {
		InstructionPurchaseDetailForm form = null;
		ProductView productView = null;		
		ProductService service = null;
		List products = null;
		
		form =  (InstructionPurchaseDetailForm)actionForm;
		productView = form.getIpdView().getProductViewSearch();
		service = new ProductService();		
		products = service.getProductList(productView);		
		form.getIpdView().setProductList(products);
		request.setAttribute("listOfProducts", form.getIpdView().getProductList());		
		return mapping.findForward(SUCCESS_FORWARD);
	} 
	
	/**
	 * 
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InstructionPurchaseDetailException
	 * @throws InstructionsForecastsException
	 */
	public ActionForward selectProduct(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws ProductException {
		InstructionPurchaseDetailForm instructionPurchaseDetailForm = null;
		HttpSession httpSession = null;		
		MessageResources messageResources = null;
		
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			instructionPurchaseDetailForm =  (InstructionPurchaseDetailForm) actionForm;
			if( instructionPurchaseDetailForm.getIpdView().getSelectedProducts()!=null && 
				instructionPurchaseDetailForm.getIpdView().getSelectedProducts().length>0 &&
				instructionPurchaseDetailForm.getIpdView().getProductList()!=null
			){
				List idsSelectedProducts = new ArrayList(0);
				for(int i=0;i<instructionPurchaseDetailForm.getIpdView().getSelectedProducts().length;i++){
					idsSelectedProducts.add((String)instructionPurchaseDetailForm.getIpdView().getSelectedProducts()[i]);
				}
				List selectedProducts = new ArrayList(0);
				Iterator iterator = instructionPurchaseDetailForm.getIpdView().getProductList().iterator();
				while (iterator.hasNext()) {
					ProductView product = (ProductView) iterator.next();
					if(idsSelectedProducts.contains(product.getProductId())){
						selectedProducts.add(product);
					}
				}
				httpSession.setAttribute(SELECTED_PRODUCTS, selectedProducts);
				httpSession.setAttribute(NSELECTED_PRODUCTS, new Integer(selectedProducts.size()));
			}else{
				String messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), MESSAGE_SELECT_A_PRODUCT);
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
			}
		}catch (Exception exception){
			ProductException productException=null;
			exception.printStackTrace(System.out);
			productException= new ProductException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw productException;
		}
		return mapping.findForward(SUCCESS_FORWARD);
	} 
	
	
}
